<?php
class post extends spController
{
	function index(){ // 这里是首页
		$this->tpl_title = "文章管理";
		// 这里使用了spPager，同时用spArgs接受到传入的page参数  
		$post= spClass("lib_contents");  
        if ($_SESSION['userinfo']['acl']=='administrator') {
         $condition=array('type'=>'post');
        }
        else
        {$condition=array('type'=>'post','author'=>$_SESSION['userinfo']['name']);
		}
        $this->results = $post->spPager($this->spArgs('page', 1), 20)->findAll($condition,"id DESC");   
		$this->select = spClass("lib_category")->findAll();
		// 这里获取分页数据并发送到smarty模板内  
		$this->pager = $post->spPager()->getPager();
		$this->display("admin/post_index.html");
	}
	
	function write(){ // 撰写新文章
		$this->tpl_title = "撰写新文章";
		$this->created = date("Y-m-d H:i:s");
		$this->formUrl = spUrl("post", "add");
		$this->select = spClass("lib_category")->findAll();
		$this->display("admin/post_write.html");
	}	

	function edit(){ // 修改
		$this->tpl_title = "编辑文章";
		$this->formUrl = spUrl("post", "update");
		$this->select = spClass("lib_category")->findAll();
		// 这里先判断是否传入了id
		if( $id = $this->spArgs("id") ){
			// 查找表，获取信息
			$this->info = spClass("lib_contents")->find(array('id'=>$id));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("post","index"));
		}
		$this->display("admin/post_write.html");
	}	
	
	function add(){ // 增加
		$user = spClass("lib_contents");
		$newrow = array( // 这里制作新增记录的值
			'parent' => $this->spArgs('parent'), 
			'title' => $this->spArgs('title'),
			'slug' => $this->spArgs('slug'),
			'thumb' => $this->spArgs('thumb'),
			'created' => $this->spArgs('created'),
			'text' => $this->spArgs('text'),
			'status' => $this->spArgs('status',0),
			'public' => $this->spArgs('public',0),
			'sort' => $this->spArgs('sort'),
			'overview' => $this->spArgs('overview'),
			'allowCommend' => $this->spArgs('allowCommend'),
			'author' => $_SESSION["userinfo"]["name"],
			'type' => 'post',
		);
		$user->create($newrow); 
		$this->success("文章撰写成功！", spUrl("post", "index"));
	}	

	function update(){ //更新
		$post = spClass("lib_contents");
		// 这里先判断是否传入了id
		if( $id = $this->spArgs("id") ){
		$conditions = array("id"=>$id); 
		$newrow = array(
			'parent' => $this->spArgs('parent'), 
			'title' => $this->spArgs('title'),
			'slug' => $this->spArgs('slug'),
			'thumb' => $this->spArgs('thumb'),
			'created' => $this->spArgs('created'),
			'modified' => date("Y-m-d H:i:s"),
			'text' => $this->spArgs('text'),
			'status' => $this->spArgs('status',0),
			'public' => $this->spArgs('public',0),
			'sort' => $this->spArgs('sort'),
			'allowCommend' => $this->spArgs('allowCommend'),
//			'author' => $_SESSION["userinfo"]["name"],
			);
		$post->update($conditions, $newrow); // 更新记录
		$this->success("更新文章成功！", spUrl("post", "index"));
		}
	}
	
	function cate(){ // 浏览分类下文章
		$this->tpl_title = "文章管理";
		$post= spClass("lib_contents");  
		$id = $this->spArgs("id");
		$this->id = $this->spArgs("id");
		$this->results = $post->spPager($this->spArgs('page', 1), 20)->findAll(array("type" => 'post', "parent" =>$id));   
		$this->select = spClass("lib_category")->findAll();
		// 这里获取分页数据并发送到smarty模板内  
		$this->pager = $post->spPager()->getPager();
		$this->display("admin/post_cate.html");
	}	

	function check(){
		// 接收提交的slug值
		$slug = $this->spArgs('slug');
		$results = spClass('lib_contents')->find(array( 'slug' => $slug )); // 查找
		$slugs = $results['slug'];

		if( $slug == $slugs ){
			$result = array(
				'status' => 0, // 失败标志
				'message' => '<span class="input-notification error png_bg">缩略名重复</span>', // 提示信息
			);		
		}else{
			$result = array(
				'status' => 1, // 成功标志
				'message' => '<span class="input-notification success png_bg">缩略名可用</span>', // 提示信息
			);	
		}
		echo json_encode( $result ); // 返回（显示）JSON结果
	}
	
	function del(){// 删除
		// 这里先判断是否传入了id
		if( $id = $this->spArgs("id") ){
			// 执行删除
			spClass("lib_contents")->delete(array('id'=>$id));
			$this->success("删除成功！", spUrl("post","index"));
		}else{
			// 无id则直接跳转回首页
			$this->jump(spUrl("post","index"));
		}
	}
	
}